package net.softwarefabrik.bde.core.model.maven;

import net.softwarefabrik.bde.core.model.BDEConstants;
import net.softwarefabrik.bde.core.model.BDEException;

/**
 * A class for storing information to identify an artifact in a
 * Maven 2 repository.
 * 
 * @author Matthias Kuespert
 */
public class ArtifactInfo {

    private String artifactId = null;

    private String groupId    = null;

    private String version    = null;

    private String extension  = null;

    /**
     * Initializing constructor.
     * 
     * @param artifactId The artifactId.
     * @param groupId The groupId.
     * @param version The version.
     * @param extension The extension of the artifact.
     */
    public ArtifactInfo(String artifactId, String groupId, String version, String extension) {
        this.artifactId = artifactId;
        this.groupId = groupId;
        this.version = version;
        this.extension = extension;
    }

    /**
     * Creates an <code>ArtifactInfo</code> from a given location.
     * 
     * @param mavenLocation The relative location of the artifact in a Maven2
     *        repository. Note: directory separators must be given in Unix
     *        notation (/)
     * @throws BDEException If the given location could not be parsed.
     */
    public ArtifactInfo(String mavenLocation) throws BDEException {
        String[] parts = mavenLocation.split(BDEConstants.STD_FILE_SEPARATOR);
        if (parts.length < 4) {
            throw new BDEException("Could not parse location: " + mavenLocation);
        }
        // TODO: debug
        this.version = parts[parts.length - 2];
        this.artifactId = parts[parts.length - 3];
        this.groupId = BDEConstants.EMPTY_STRING;
        for (int i = 0; i < parts.length - 3; i++) {
            if (!BDEConstants.EMPTY_STRING.equals(this.groupId)) {
                this.groupId += BDEConstants.STD_PACKAGE_SEPARATOR;
            }
            this.groupId += parts[i];
        }
        String fileName = parts[parts.length - 1];
        this.extension = fileName.substring(fileName.lastIndexOf("."));
        if (!fileName.equals(this.artifactId
                             + BDEConstants.STD_VERSION_SEPARATOR
                             + this.version
                             + BDEConstants.STD_EXTENSION_SEPARATOR
                             + this.extension)) {
            throw new BDEException("Could not parse location: "
                                   + mavenLocation
                                   + "\nUnknown artifact file name format: "
                                   + fileName);
        }
        //
        // String groupId = "";
        // for (int i = 0; i < parts.length - 3; i++) {
        // if (!"".equals(groupId)) {
        // groupId += ".";
        // }
        // groupId += parts[i];
        // }
        // this.groupId = groupId;
        // //
        // int idxDot = parts[parts.length - 1].lastIndexOf(".");
        // if (idxDot >= 0) {
        // this.extension = parts[parts.length - 1].substring(idxDot + 1);
        // } else {
        // throw new BDEException("No extension found in: " + mavenLocation);
        // }
        // //
        // int idxHyphen = parts[parts.length - 1].lastIndexOf("-");
        // if (idxHyphen >= 0) {
        // this.version = parts[parts.length - 1].substring(idxHyphen + 1,
        // idxDot);
        // this.artifactId = parts[parts.length - 1].substring(0, idxHyphen);
        // } else {
        // throw new BDEException("No version found in: " + mavenLocation);
        // }
    }

    /**
     * @return A <code>String</code> representation of this object.
     */
    public String toString() {
        return getGroupId()
               + BDEConstants.STD_PACKAGE_SEPARATOR
               + getArtifactId()
               + BDEConstants.STD_VERSION_SEPARATOR
               + getVersion()
               + BDEConstants.STD_EXTENSION_SEPARATOR
               + getExtension();
    }

    /**
     * @return The relative location of this artifact in a Maven2 repository.
     */
    public String getMavenLocation() {
        String result = BDEConstants.STD_FILE_SEPARATOR + getGroupId();
        result = result.replaceAll("\\" + BDEConstants.STD_PACKAGE_SEPARATOR,
                                   BDEConstants.STD_FILE_SEPARATOR);
        result += BDEConstants.STD_FILE_SEPARATOR
                  + getArtifactId()
                  + BDEConstants.STD_FILE_SEPARATOR
                  + getVersion()
                  + BDEConstants.STD_FILE_SEPARATOR
                  + getArtifactId()
                  + BDEConstants.STD_VERSION_SEPARATOR
                  + getVersion()
                  + BDEConstants.STD_EXTENSION_SEPARATOR
                  + getExtension();
        return result;
    }

    /**
     * @return the artifactId
     */
    public String getArtifactId() {
        return artifactId;
    }

    /**
     * @return the groupId
     */
    public String getGroupId() {
        return groupId;
    }

    /**
     * @return the version
     */
    public String getVersion() {
        return version;
    }

    /**
     * @return the extension
     */
    public String getExtension() {
        return extension;
    }
}
